package cn.dnui_yxk801.dnuifood_16110100801.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.CommentsBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.controller.ModifyCommentsActivity;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.UserModel;

public class MyCommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnRetrofitListener{
    private Context context;
    private String item_id;
    private List<CommentsBean> datalist;
    private LayoutInflater layoutInflater;
    private static String Baseurl="http://172.24.10.175:8080/foodService/";

    public MyCommentsAdapter(Context context, List datalist)
    {
        this.context=context;
        this.datalist=datalist;
        layoutInflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v=layoutInflater.from(viewGroup.getContext()).inflate(R.layout.mycommentsitem,viewGroup,false);
        return new MyCommentsAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        final CommentsBean entity=(CommentsBean)datalist.get(i);
        if (entity==null)
            return;
        item_id=entity.getItem_id();
        MyCommentsAdapter.ViewHolder viewHolder=(MyCommentsAdapter.ViewHolder)holder;
        viewHolder.username.setText(entity.getUsername());
        viewHolder.shopname.setText(entity.getShopname());
        viewHolder.foodname.setText(entity.getFoodname());
        viewHolder.time.setText(entity.getComment_time());
        viewHolder.sum.setText(entity.getSum());

        viewHolder.modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ModifyCommentsActivity.class);
                String content=entity.getContent();
                int flag=0;
                intent.putExtra("flag",flag);
                intent.putExtra("content",content);
                intent.putExtra("item_id",item_id);
                context.startActivity(intent);
            }
        });
        viewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deletecomment(item_id);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datalist.size();
    }

    @Override
    public void onSuccess(Object object, int flag) {
        if (flag==10){
            ResultBean resultBean=(ResultBean)object;
            if (resultBean.getSuccess().equals("1")) {
                Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();
            }
            else
                Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFalure(String msg) {

    }

    private void deletecomment(String item_id){
        UserModel userModel=new UserModel();
        userModel.deleteComment(item_id,this);
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView username,shopname,foodname,time,sum;
        Button modify,delete;
        //ImageView food_pic;


        public ViewHolder(View view) {
            super(view);
            username=(TextView)view.findViewById(R.id.mc_tv_uname);
            shopname=(TextView)view.findViewById(R.id.mc_tv_shopname);
            foodname=(TextView)view.findViewById(R.id.mc_tv_foodname);
            time=(TextView)view.findViewById(R.id.mc_tv_time);
            sum=(TextView)view.findViewById(R.id.mc_tv_sum);
            modify=(Button)view.findViewById(R.id.mc_btn_modify);
            delete=(Button)view.findViewById(R.id.mc_btn_delete);

        }
    }
}
